package projectmanagement.model;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

@Entity
public class Task implements Serializable{

	private static final long serialVersionUID = 1;
	
	@Id
	@GeneratedValue
	private int id;
	
	@ManyToOne
	@JoinColumn(name="project")
	private Project project;
	
	@ManyToOne
	@JoinColumn(name="assignee")
	private Person assignee;
	
	private String header;
	private Date creationDate;
	private Date deadlineDate;
	private String status;
	private String comment;
	
	@Transient
	private String projectStr;
	
	@Transient
	private String assigneeStr;
	
	@Transient
	private String deadlineDateStr;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getHeader() {
		return header;
	}
	public void setHeader(String header) {
		this.header = header;
	}
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	public Person getAssignee() {
		return assignee;
	}
	public void setAssignee(Person assignee) {
		this.assignee = assignee;
	}
	public Date getCreationDate() {
		return creationDate;
	}
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	public Date getDeadlineDate() {
		return deadlineDate;
	}
	public void setDeadlineDate(Date deadlineDate) {
		this.deadlineDate = deadlineDate;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public String getProjectStr() {
		return String.valueOf(project.getId());
	}
	public void setProjectStr(String projectStr) {
		this.projectStr = projectStr;
		project = new Project();
		project.setId(Integer.parseInt(projectStr));
	}
	public String getAssigneeStr() {	
		return String.valueOf(assignee.getId());
	}
	public void setAssigneeStr(String assigneeStr) {
		this.assigneeStr = assigneeStr;
		
		assignee = new Person();
		assignee.setId(Integer.parseInt(assigneeStr));
	}
	
	public String getDeadlineDateStr() {
		if(deadlineDate != null){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			this.deadlineDateStr = df.format(deadlineDate);
		}
		
		return deadlineDateStr;
	}
	
	public void setDeadlineDateStr(String deadlineDateStr) {
		this.deadlineDateStr = deadlineDateStr;
		
		if (deadlineDateStr == null || deadlineDateStr.trim().length() <= 0){
			deadlineDate = null;
		}else{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			
			try {
				deadlineDate = df.parse(deadlineDateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}
}
